package com.hxgis.oc.product.geojson;

import cn.hutool.json.JSONObject;
import com.hxgis.oc.product.base.ConvertTask;
import com.hxgis.oc.product.idw.IDWConvTask;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.function.Function;

@Slf4j
@Getter
@Setter
public class GeojsonConvTask extends ConvertTask {
    protected Function<File, JSONObject> convFunc;
    protected IDWConvTask idwConvTask;

    public GeojsonConvTask(Long workID, String ds) {
        super(workID,ds);
        productType = GeojsonProductManager.getFactoryName();
    }

    @Override
    public boolean producing() {
        boolean succeed = false;
        try {
            if(convFunc != null){
                JSONObject jsonObject = convFunc.apply(inputFile);
                if(jsonObject != null){
                    FileOutputStream fos = new FileOutputStream(outputFile);
                    fos.write(jsonObject.toString().getBytes(StandardCharsets.UTF_8));
                    fos.close();
                    succeed = true;
                }
            }
        } catch (IOException e) {
            log.error(e.getMessage());
        }finally {
            if(!succeed){
                retrieve();
            }
        }
        return succeed;
    }
}
